package utilities;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public final class CommandFileParser {
	private static final String WHITESPACE_PLACE_HOLDER = "~_~";
	
	public static List<String> parseFile(BufferedReader reader)
			throws IOException {
		List<String> cmdList = new ArrayList<String>();
		StringBuffer cmdBuffer = new StringBuffer();
		String line = null;
		while (null != (line = reader.readLine())) {
			if(line.matches("\\s") ||line.isEmpty()){
				continue;
			}
			line = trimLeadingWhitespace(line);
			// check if first char is a '#'
			if (isComment(line)) {
				// if so, ignore the line
				continue;
			}

			// if first word is an operation word, start a new cmd string
			if (isOperationWord(line)) {
				if (0 < cmdBuffer.length()) {
					cmdList.add(cmdBuffer.toString().replace(WHITESPACE_PLACE_HOLDER, " ") );
					cmdBuffer = new StringBuffer();
				}
			}
			
			//Check for quoted values; ignoring \"
			//Replace ' ' in quotes with '~_~'
			line = fillWhitespaceInQuotes(line);

			// continue to append to current cmd string
			for (String token : line.split("\\s")) {
				cmdBuffer.append(ArgumentParser.TOKEN_DELIMITER);
				cmdBuffer.append(token);
				cmdBuffer.append(ArgumentParser.TOKEN_DELIMITER + " ");
			}

		}
		if (0 < cmdBuffer.length()) {
			cmdList.add(cmdBuffer.toString().replace(WHITESPACE_PLACE_HOLDER, " "));
		}
		return cmdList;

	}
	
	public static String fillWhitespaceInQuotes(String line){
		StringBuffer lineBuffer = new StringBuffer(line);
		boolean inQuotes = false;
		
		for(int index = 0; index < lineBuffer.length(); index++){
			if( '"' == lineBuffer.charAt(index)){
				if(0 < index){
					if('\\' != lineBuffer.charAt( index - 1 )){
						if(inQuotes){
							inQuotes = false;
						}else{
							inQuotes = true;
						}
						lineBuffer.replace(index, index + 1, "");
						index--;
					}
				}
			}
			else if( ' ' == lineBuffer.charAt(index) ) {
				if(inQuotes){
					lineBuffer.replace(index, index + 1, WHITESPACE_PLACE_HOLDER);
					index += 2;
				}
			}
		}
		return lineBuffer.toString();
	}

	public static boolean isOperationWord(String line) {
		boolean result = false;
		for (String word : line.split("\\s")) {
			if (null == word) {
				continue;
			} else {
				if ("help".equalsIgnoreCase(word)
						|| "change".equalsIgnoreCase(word)
						|| "swap".equalsIgnoreCase(word)
						|| "attribute".equalsIgnoreCase(word)
						|| "analyze".equalsIgnoreCase(word)) {
					result = true;
				}

				break;
			}

		}
		return result;
	}

	public static boolean isComment(String line) {
		if (line.startsWith("#")) {
			return true;
		} else {
			return false;
		}
	}

	public static String trimLeadingWhitespace(String line) {
		String trimmedString = line;
		while (true) {
			if (' ' == trimmedString.charAt(0) || '\t' == trimmedString.charAt(0)) {
				trimmedString = trimmedString.substring(1);
			} else {
				break;
			}
		}
		return trimmedString;
	}
}
